package lovo.practice_7_14.frame;

import lovo.Util.Util;
import lovo.practice_7_14.dao.UserDAO;
import lovo.practice_7_14.dao.impl.UserDAOImpl;
import lovo.practice_7_14.service.UserService;
import lovo.practice_7_14.service.impl.UserService.UserServiceImpl;
import lovo.practice_7_14.util.DBUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SavePanel extends JPanel implements ActionListener {
    private JButton jButtonSave;
    private JTextField jTextFieldSave;
    private J189_7_14 frame;
    private Icon imgback = Util.getimagesIcon("/Images/buttonOff.jpg");
    private JButton jButtonBack;
    private Icon ImageSave = Util.getimagesIcon("/Images/buttonSave.jpg");

    public SavePanel(J189_7_14 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jButtonSave = new JButton(ImageSave);
        jButtonSave.setBounds(200, 200, 140, 50);
        this.add(jButtonSave);

        jTextFieldSave = new JTextField();
        jTextFieldSave.setBounds(400, 200, 140, 50);
        this.add(jTextFieldSave);


        jButtonBack = new JButton(imgback);
        jButtonBack.setBounds(500, 400, 140, 50);
        this.add(jButtonBack);
        jButtonBack.addActionListener(this);
        jButtonSave.addActionListener(this);

    }

//    /**
//     * 创建连接 t_atm
//     *
//     * @return
//     */
//    public Connection getConnection() {
//        // 1. 加载驱动
//        Connection con = null;
//        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            // 2. 创建连接
//            con = DriverManager.getConnection(
//                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
//                    "root", "0201");
//
//        } catch (ClassNotFoundException | SQLException e) {
//            e.printStackTrace();
//        }
//        return con;
//    }

//    /**
//     * 修改数据库的值
//     */
//    public void cha() {
//
//        // 1.创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//
//        try {
//            // 2 获取执行SQL语句对象
//            statement = con.createStatement();
//            String str=frame.getUsers().getAccount();
//            String sql = " update t_atm set a_balance =" + frame.getUsers().getBalance() + " where a_account='"+str+"' ";
//            System.out.println(sql);
//            //执行SQL语句
//            int row = statement.executeUpdate(sql);
//            System.out.println("影响行数：" + row);
//
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            //4 关闭连接
//            if (statement != null) {
//                try {
//                    statement.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//            if (con != null) {
//                try {
//                    con.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//
//
//        }
//    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtonBack) {
            frame.switchPanel("mune");
        } else if (e.getSource() == jButtonSave) {
//            frame.getUsers().setBalance(frame.getUsers().getBalance() + Integer.parseInt(this.jTextFieldSave.getText()));
//
//            //把修改的值修改到数据库
////           frame.getLoginPanel().cha();
//            DBUtil dbUtil = new DBUtil();
//
//            dbUtil.update(frame.getUsers());
//            JOptionPane.showMessageDialog(this, "存款成功");
//            frame.getSavePanel().jTextFieldSave.setText("");
//
            double saveBalance =Double.valueOf(jTextFieldSave.getText());
            UserService userService =new UserServiceImpl();
            userService.saveMoney(frame.getUsers().getAccount(),saveBalance);
            JOptionPane.showMessageDialog(this, "存款成功");

        }
    }


}
